﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TopShooter
{
	public enum TileCollision
	{
		Path = 0,
		Normal = 1,
		Blocked = 2,
		NonPath = 3,
	}

	public class Tile
	{
		public char MapCode;
		public Texture2D Texture;
		public TileCollision Collision;
		public const int Width = 24;
		public const int Height = 24;
		public static readonly Point Size = new Point(Width, Height);
		public static readonly Point HalfSize = new Point(Width / 2, Height / 2);
		public string TextureName;

		static private int s_NextID = 1;

		public Tile(string textureName, TileCollision collision, char mapCode)
			: this(textureName, collision)
		{
			MapCode = mapCode;
		}

		public Tile(string textureName, TileCollision collision)
		{
			TextureName = textureName;

			Collision = collision;
		}
		public virtual void LoadContent(Scene scene)
		{
			if (TextureName != null)
				Texture = Game.Instance.Content.Load<Texture2D>(TextureName);
		}
		public override string ToString()
		{
			return "tile:" + MapCode;
		}
		public virtual void Draw(SpriteBatch spriteBatch, Vector2 offset, int column, int row)
		{
			Texture2D texture = Texture;
			if (texture != null)
			{
				// Draw it in screen space.
				Vector2 loc = new Vector2(column * Tile.Size.X, row * Tile.Size.Y);
				spriteBatch.Draw(texture, loc + offset, Color.White);
			}
		}

		public virtual void OnAdding(Scene scene, Coordinate loc)
		{
		}

		public void OnAdded(Scene scene, int x, int y)
		{
			_OnAdded(scene, x, y);
		}
		protected virtual void _OnAdded(Scene scene, int x, int y)
		{
		}

		public virtual void OnRemoved(Scene scene, int column, int row)
		{
		}
		public virtual string GetDefinition()
		{
			return "";
		}

		public virtual void OnEntityCollided(Entity entity)
		{
		}

		public virtual void HandleDamage(int x, int y, float damage, Scene scene)
		{
		}

		public virtual string SetDefinition(string def)
		{
			return null;
		}
	}
}
